﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Collections;
using System.Data.SqlClient;
using System.Configuration;

namespace RAgency.Admin
{
    public partial class ADelUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetUsers();
            }
        }

        protected void Delete_User(object sender, EventArgs args)
        {
            Membership.DeleteUser(UsersDDL.Text);
            SQLDeleteUser(UsersDDL.Text);
            GetUsers();
        }

        private void GetUsers()
        {
            string[] users_m = Roles.GetUsersInRole("manager");
            string[] users_u = Roles.GetUsersInRole("user");
            UsersDDL.DataSource = users_m.Concat(users_u);
            UsersDDL.DataBind();
        }

        private void SQLDeleteUser(string login)
        {
            SqlConnection conn_user = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
            SqlConnection conn_results = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
            try
            {
                conn_user.Open();
                conn_results.Open();
                
                SqlCommand comm_r = new SqlCommand("DELETE FROM results WHERE (id = (SELECT id FROM users WHERE login='" + login + "'))", conn_results);
                comm_r.ExecuteNonQuery();

                SqlCommand comm_u = new SqlCommand("DELETE FROM users WHERE (login = '" + login + "')", conn_user);
                comm_u.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ErrLabel.Text = ex.Message;
            }
            finally
            {
                conn_user.Close();
                conn_results.Close();
            }
        }
    }
}